package com.song.mall.order.listener;

import com.rabbitmq.client.Channel;
import com.song.common.to.mq.SeckillOrderTo;
import com.song.mall.order.entity.OrderEntity;
import com.song.mall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2021/10/18 - the current system date.
 * 17:00 - the current system time.
 */
@Slf4j
//监听那个队列
@RabbitListener(queues ="order.seckill.order.queue" )
@Component
public class OrderSeckillListenter {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrder, Message message, Channel channel) throws IOException {
        //System.out.println("收到过期的订单 准备关闭订单"+entity.getStatus());
        try {
            log.info("准备创建秒杀单的详细信息。。。。");
            orderService.createSeckillOrder(seckillOrder);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
